<template>
  <el-upload v-model:file-list="fileList" :limit="limit" action="" :http-request="customHttp" drag class="a-upload">
    <div class="el-upload__text">将文件拖拽到此处，或<em>点击选择文件</em></div>
  </el-upload>
</template>

<script lang="ts" setup generic="T extends UploadFile">
defineOptions({
  name: 'a-upload'
})

interface Props {
  limit?: number
  httpRequest?: (data: FormData) => Promise<unknown>
}

const fileList = defineModel<T[]>()

const props = withDefaults(defineProps<Props>(), {
  limit: 1,
  httpRequest: () => Promise.resolve()
})

async function customHttp(options: UploadRequestOptions) {
  const formData = new FormData()
  formData.append('file', options.file)
  return props.httpRequest(formData)
}
</script>

<style lang="scss">
.a-upload {
  width: 100%;
  .el-upload__text {
    font-size: $font-size-small !important;
  }
}
</style>
